<?php
class category_post_model extends my_model{

	function __construct(){
		parent::__construct();
		$this->_prefix = "ctp";
		$this->_pk = "";
		$this->_column = array(
													'cat_id'	=>	$this->_prefix."_cat_id",
													'pos_id'	=>	$this->_prefix."_pos_id"
		);
		$this->_join_column = array(
													'id'								=> "pos_id",
													'title'							=> "pos_title",
													'content'						=> "pos_content",
													'usr_ip'						=> "pos_usr_ip",
													'status'						=> "pos_status",
													'comment_status'		=> "pos_comment_status",
													'usr_id'						=> "pos_usr_id",
													'cat_id'						=> "pos_cat_id",
													'typ_id'						=> "pos_typ_id",
													'cr_date'						=> "pos_cr_date",
													'lu_date'						=> "pos_lu_date",
													'lu_uid'						=> "pos_lu_uid",
													'update_date'				=> "pos_update_date",
													'url'								=> "pos_url",
													'type'							=> "pos_type",
													'code'							=> "pos_code",
													'view'							=> "pos_view"
		);
	}
	function get($options = NULL){
    $this->db->join("ci_post","ci_category_post.ctp_pos_id = ci_post.pos_id");
    $this->db->where("pos_type","post");
    $posts = parent::get($options);
    if(!empty($posts)){
    	$this->load->model("images_model","imagesModel");
			foreach ($posts as $key => $value) {
				if(!empty($value["id"])){
					$posts[$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
				}
				
				$posts[$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
				$posts[$key]["meta"] = Util::getPostMeta($posts[$key]["id"]);
				$posts[$key]["type"] = Util::getType($posts[$key]["typ_id"]);
				$posts[$key]["comment_count"] = Util::getCommentCount($posts[$key]["id"]);
				$posts[$key]["user"] = $this->ion_auth->user_array($posts[$key]["usr_id"]);
			}
		}
    return $posts;
  }

  public function getCategorySelectedByPosId($posId = NULL){
  	$where["where"]["pos_id"] = $posId;
  	return parent::get($where);
  }
}

?>